﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 温湿度和颗粒度监控测试.Common;
using 温湿度和颗粒度监控测试.Common.传感器;
using System.Windows.Input;

namespace 温湿度和颗粒度监控测试.ViewModel.颗粒度
{
    public class 颗粒度测试WindowViewModel : NotifyBase
    {
        public delegate void MessageHandler(string msg);
        public MessageHandler ErrorMessageEvent;
        public MessageHandler InfoMessageEvent;


        private string _OutputText;
        /// <summary>
        /// 输出文本
        /// </summary>
        public string OutputText { get => _OutputText; set => SetProperty(ref _OutputText, value); }

        温湿度记颗粒度传感器 温湿度记颗粒度传感器Client;

        private bool _ModbusClientIsConnected = false;
        /// <summary>
        /// Modbus客户端是否连接状态
        /// </summary>
        public bool ModbusClientIsConnected { get => _ModbusClientIsConnected; set => SetProperty(ref _ModbusClientIsConnected, value); }

        public ICommand 打开串口 => new CommandBase(param =>
        {
            try
            {
                if (温湿度记颗粒度传感器Client == null) 温湿度记颗粒度传感器Client = new 温湿度记颗粒度传感器();

                if (ModbusClientIsConnected)
                {
                    // 断开串口
                    OutputText += $"[{DateTime.Now:HH:mm:ss}]关闭串口\r\n";

                    温湿度记颗粒度传感器Client.Close();
                    ModbusClientIsConnected = false;
                }
                else
                {
                    // 打开串口
                    OutputText += $"[{DateTime.Now:HH:mm:ss}]打开串口\r\n";

                    温湿度记颗粒度传感器Client.PortName = "COM4";
                    ModbusClientIsConnected = true;
                }
            }
            catch (ApplicationException ex)
            {
                ErrorMessageEvent?.Invoke(ex.Message);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                ErrorMessageEvent?.Invoke(ex.Message);
            }
        });

        public ICommand 读取AM1050的PM03 => new CommandBase(param =>
        {
            try
            {
                if (温湿度记颗粒度传感器Client == null) throw new ApplicationException("未打开串口，请先打开串口");

                int 颗粒度 = 温湿度记颗粒度传感器Client.获取AM1050的PM05颗粒度(0x32);

                OutputText += $"[{DateTime.Now:HH:mm:ss}]AM1050的PM0.3：{颗粒度}\r\n";
            }
            catch (ApplicationException ex)
            {
                ErrorMessageEvent?.Invoke(ex.Message);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                ErrorMessageEvent?.Invoke(ex.Message);
            }
        });

        public ICommand 读取OPC6303M的PM05 => new CommandBase(param =>
        {
            try
            {
                if (温湿度记颗粒度传感器Client == null) throw new ApplicationException("未打开串口，请先打开串口");

                int 颗粒度 = 温湿度记颗粒度传感器Client.获取OPC6030M的PM05(0x31);

                OutputText += $"[{DateTime.Now:HH:mm:ss}]OPC6303M的PM0.5：{颗粒度}\r\n";
            }
            catch (ApplicationException ex)
            {
                ErrorMessageEvent?.Invoke(ex.Message);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                ErrorMessageEvent?.Invoke(ex.Message);
            }
        });
    }
}
